package com.kingsoft.gtdsys.dao.impl.sqllite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.kingsoft.gtdsys.dao.IReferenceDAO;
import com.kingsoft.gtdsys.entity.Reference;
import com.kingsoft.gtdsys.util.GlobalValues;
import com.kingsoft.gtdsys.util.Log;

public class ReferenceDAO implements IReferenceDAO {
	public static final String TABLE_NAME = "t_references";
	private static final String[] COLS = new String[]{ "_id", "content", "date",
														"organized", "type" };
	
	private SQLiteDatabase db;
	private DBOpenHelper dbOpenHelper;
	
	public ReferenceDAO() {
		this.dbOpenHelper = new DBOpenHelper(GlobalValues.context);
		this.establishDb();
	}
	
	private void establishDb() {
		if (db == null) {
			db = dbOpenHelper.getWritableDatabase();
			dbOpenHelper.onCreate(db);
		}
	}
	
	public void cleanup() {
		if (db != null) {
			db.close();
			db = null;
		}
	}
	
	public void insert(Reference ref) {
		try{
			ContentValues values = new ContentValues();
			values.put("content", ref.getContent());
			values.put("date", ref.getDate());
			values.put("organized", ref.getOrganized());
			values.put("type", ref.getType());
			db.insert(TABLE_NAME, null, values);
		} catch (Exception e) {
			Log.e(e.toString());
		}
	}
	
	@Override
	public List<Reference> findAll() {
		List<Reference> refs = new ArrayList<Reference>();
		Cursor c = null;
		try {
			c = db.query(TABLE_NAME, COLS, null, null, null, null, null);
			int rowCount = c.getCount();
			c.moveToFirst();
			for (int i = 0; i < rowCount; i++) {
				Reference ref = new Reference();
				ref.setId(c.getInt(0));
				ref.setContent(c.getString(1));
				ref.setDate(c.getString(2));
				ref.setOrganized(Boolean.valueOf(c.getString(3)));
				ref.setType(c.getString(4));
				refs.add(ref);
				c.moveToNext();
			}
		} catch (SQLException e) {
			Log.e(e.toString());
		} finally {
			if (c != null && !c.isClosed()) {
				c.close();
			}
		}
		return refs;
	}

	@Override
	public void delete(int id) {
		db.delete(TABLE_NAME, "_id=" + id, null);		
	}

	@Override
	public void update(Reference ref) {
		ContentValues values = new ContentValues();
		values.put("content", ref.getContent());
		values.put("date", ref.getDate());
		values.put("organized", ref.getOrganized());
		values.put("type", ref.getType());
		db.update(TABLE_NAME, values, "_id=" + ref.getId(), null);		
	}
	
	@Override
	public Reference findById(int id) {
		Reference ref = null;
		Cursor c = null;
		try {
			c = db.query(TABLE_NAME, COLS, "_id=" + id, null, null, null, null);
			if (c.getCount() > 0) {
				c.moveToFirst();
				ref = new Reference();
				ref.setId(c.getInt(0));
				ref.setContent(c.getString(1));
				ref.setDate(c.getString(2));
				ref.setOrganized(Boolean.valueOf(c.getString(3)));
				ref.setType(c.getString(4));
			}
		} catch (Exception e) {
			Log.v(e.toString());
		} finally {
			if (c != null && !c.isClosed()) {
				c.close();
			}
		}
		return ref;
	}
}
